package com.sykj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sykj.dto.OrderMasterDto;
import com.sykj.entity.OrderMaster;
import com.sykj.mapper.OrderMasterMapper;
import com.sykj.service.IOrderDetailService;
import com.sykj.service.IOrderMasterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sykj
 * @since 2019-06-28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements IOrderMasterService {

    @Autowired
    private IOrderDetailService detailService;

    @Override
    public String createOrder(OrderMasterDto orderMasterDto) {
        OrderMaster master = new OrderMaster();
        BeanUtils.copyProperties(orderMasterDto, master);
        String orderId = String.valueOf(System.currentTimeMillis());
        master.setBuyerOpenid("openid");
        master.setOrderId(orderId);
        this.baseMapper.insert(master);
        // 循环对象中的每一个商品,添加订单详情
        orderMasterDto.getOrderDetails().forEach(detail -> {
            detail.setOrderId(orderId);
            detailService.save(detail);
        });
        return orderId;
    }
}
